// YaP IRC - Channel Info Form.
//Copyright (C) 2006  Pierre-Alain DAVID

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


using System;
using System.Windows.Forms;
using YaP.Library;

namespace YaP.Forms
{
    /// <summary>
    /// Description of ChnInfo.
    /// </summary>
    public partial class ChanInfoForm
    {
        #region Delegates

        public delegate void KeyChangedEventHandler(object sender, string key);

        public delegate void ModeEventHandler(object sender, bool set, string type);

        public delegate void UnbanEventHandler(object sender, string UnBanMask);

        public delegate void UserLimitEventHandler(object sender, int limit);

        #endregion

        #region Events

        public event UnbanEventHandler OnUnban;
        public event ModeEventHandler OnModeChange;
        public event UserLimitEventHandler OnLimitChange;
        public event KeyChangedEventHandler OnKeyChange;

        #endregion

        private ChannelChildArguments Args;
        private string channelnameField;

        public ChanInfoForm(string _ChannelName, ChannelChildArguments _args)
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            ChannelName = _ChannelName;
            Args = _args;
        }

        public string ChannelName
        {
            get { return channelnameField; }
            set
            {
                channelnameField = value;
                Text = value;
            }
        }

        private void Regen()
        {
            chkInvite.Checked = Args.modeInvite;
            chkModerated.Checked = Args.modeModerated;
            chkNoExMsg.Checked = Args.modeExternalMsg;
            chkOpsTops.Checked = Args.modeOnlySetTopic;
            chkPrivate.Checked = Args.modePrivate;
            chkSecret.Checked = Args.modeSecret;
            if (Args.UserLimit > 0)
            {
                chkLimit.Checked = true;
                txtLimit.Text = Args.UserLimit.ToString();
            }
            if ((Args.Key != null) && (Args.Key != string.Empty))
            {
                chkKey.Checked = true;
                txtKey.Text = Args.Key;
            }
            txtTopic.Text = Args.Topic;

            if (!Args.ImOp)
            {
                txtKey.ReadOnly = true;
                txtTopic.ReadOnly = true;
                chkInvite.Enabled = false;
                chkKey.Enabled = false;
                chkLimit.Enabled = false;
                chkModerated.Enabled = false;
                chkNoExMsg.Enabled = false;
                chkOpsTops.Enabled = false;
                chkPrivate.Enabled = false;
                chkSecret.Enabled = false;
                checkedListBox1.Enabled = false;
                checkedListBox2.Enabled = false;
                txtLimit.ReadOnly = true;
            }
        }

        private void ChnInfo_Load(object sender, EventArgs e)
        {
            Regen();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            Button btnSender = sender as Button;
            switch (btnSender.Name)
            {
                case "btnUnban":
                    foreach (object obj in checkedListBox1.CheckedItems)
                    {
                        (ParentForm as MainForm).DoCmd("MODE", new[] {channelnameField, "-b", obj as string});
                    }
                    break;
                case "btnRemoveInvite":
                    foreach (object o in checkedListBox2.CheckedItems)
                    {
                        (ParentForm as MainForm).DoCmd("MODE", new[] {channelnameField, "-i", o as string});
                    }
                    break;
            }
        }

        public void AddToBanList(string Mask)
        {
            checkedListBox1.Invoke((MethodInvoker) delegate { checkedListBox1.Items.Add(Mask); });
        }

        public void SetModes(string Modes, string Extra)
        {
            Args.modeExternalMsg =
                Args.modeInvite =
                Args.modeModerated = Args.modeOnlySetTopic = Args.modePrivate = Args.modeSecret = false;
            Args.Key = string.Empty;
            Args.UserLimit = 0;
            if (Modes.Length > 1)
            {
                int nbModes = Modes.Length - 1;
                for (int i = 1; i < nbModes; i ++)
                {
                    switch (Modes[i])
                    {
                        case 'm':
                            Args.modeModerated = true;
                            break;
                        case 't':
                            Args.modeOnlySetTopic = true;
                            break;
                        case 'n':
                            Args.modeExternalMsg = true;
                            break;
                        case 'i':
                            Args.modeInvite = true;
                            break;
                        case 'k':
                            Args.Key = Extra;
                            break;
                        case 'l':
                            int.TryParse(Extra, out Args.UserLimit);
                            break;
                        case 'p':
                            Args.modePrivate = true;
                            break;
                        case 's':
                            Args.modeSecret = true;
                            break;
                    }
                }
            }
            Regen();
        }
    }
}